home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_mac.hqx / SRGP port to 5.0 (compressed) / SRGP_SPHIGS Root / MacSPHIGS / sph_object.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-13  |  7.2 KB  |  295 lines

  1. #include "HEADERS.h"
  2. #include "sphigslocal.h"
  3. #include <stdlib.h>
  4.  
  5. #include "sph_object.proto.h"
  6. static void AddVertices(view_spec *, matrix, MAT3hvec *, int);
  7. static void AddObjectToEndOfList(view_spec *, obj *);
  8.  
  9.  
  10. static MAT3hvec raw_origin = {0.0,0.0,0.0,1.0};
  11. static MAT3hvec xformed_origin;
  12.  
  13.  
  14.  
  15.  
  16. extern srgp__point srgp_pdc_points[], srgp_polygon_points[];
  17.  
  18.  
  19.  
  20.  
  21.  
  22. /** OBJECT _ init
  23. Makes sure there is space allocated for "fltptVertices";
  24.    if it needs to allocate some, sets "vertexArraySize".
  25. Sets "vertexCount" to 0.
  26.  
  27. Initializes list of objects to NULL.  Initializes the chunk that will be
  28. used to falloc objects.
  29.  
  30. TO BE CALLED just before starting to traverse the network posted to
  31.   a single view.
  32. **/
  33.  
  34. void OBJECT__init (view_spec *vs)
  35. {
  36.    if (vs->uvnVertices == NULL) {
  37.       ALLOC (vs->uvnVertices, MAT3hvec, ASSUMED_NUM_OF_OBJECTS, 0);
  38.       vs->vertexArraySize = ASSUMED_NUM_OF_OBJECTS;
  39.    }
  40.    vs->vertexCount = 0;
  41.  
  42.    vs->objects = NULL;
  43.    vs->objectTail = NULL;
  44.  
  45.    vs->curTraversalIndex = 0;
  46.    
  47.    if (vs->objectChunk == NULL)
  48.       vs->objectChunk = FALLOCnew_chunk();
  49.    else
  50.       FALLOCclear_chunk (vs->objectChunk);
  51.  
  52. }
  53.  
  54.  
  55.  
  56. static void
  57. AddVertices (view_spec *vs, matrix xform, MAT3hvec *points, int count)
  58. {
  59.    register i;
  60.  
  61.    /* Check to see if enough vertices remain in global array */
  62.    if ((vs->vertexCount + count) > vs->vertexArraySize) {
  63.       vs->vertexArraySize += ASSUMED_NUM_OF_OBJECTS;
  64.       vs->uvnVertices = 
  65.      (MAT3hvec*) 
  66.         realloc (vs->uvnVertices, 
  67.              (MALLOCARGTYPE)(vs->vertexArraySize*sizeof(MAT3hvec)));
  68.       if (vs->uvnVertices == NULL)
  69.      SPH__error (ERR_MALLOC);
  70.    }
  71.  
  72.    /* Copy all the vertices to end of the uvnVertices array, performing
  73.       transformation while doing so. */
  74.    for (i=0; i<count; i++)
  75.       MAT3mult_hvec (vs->uvnVertices[vs->vertexCount++], points[i], xform, 1);
  76. }
  77.  
  78.  
  79.  
  80. static void
  81. AddObjectToEndOfList (view_spec *vs, obj *babyobj)
  82. {
  83.    if (vs->objectTail) {
  84.       vs->objectTail->next = babyobj;
  85.       vs->objectTail = babyobj;
  86.    }
  87.    else 
  88.       vs->objects = vs->objectTail = babyobj;
  89.    vs->objectTail->next = NULL;
  90. }
  91.  
  92.  
  93.  
  94. /** OBJECT__addFillArea
  95. **/
  96.  
  97. void
  98. OBJECT__addFillArea
  99.    (view_spec *vs, MAT3hvec *points, int count,
  100.     matrix xform, attribute_group *attrs)
  101. {
  102.    obj *babyobj;
  103.    int bias;
  104.    register j;
  105.    vector vec1, vec2;
  106.  
  107.    bias = vs->vertexCount;
  108.  
  109.    AddVertices (vs, xform, points, count);
  110.  
  111.    /* Create a single facet objects. */
  112.    babyobj = (obj*)
  113.       FALLOCalloc (vs->objectChunk, sizeof(obj), FALLOC__DONT_ZERO);
  114.    babyobj->type = objFace;
  115.    babyobj->attributes = *attrs;
  116.    babyobj->data.face.numPoints = count;    
  117.    babyobj->data.face.points = 
  118.       (vertex_index*)
  119.      FALLOCalloc (vs->objectChunk,
  120.               sizeof(vertex_index) * babyobj->data.face.numPoints,
  121.               FALLOC__DONT_ZERO);
  122.    for (j=0; j < babyobj->data.face.numPoints; j++)
  123.       babyobj->data.face.points[j] = j + bias;
  124.  
  125.    /* Compute normal */
  126.    MAT3_SUB_VEC (vec1, 
  127.          vs->uvnVertices[bias+1], vs->uvnVertices[bias]);
  128.    MAT3_SUB_VEC (vec2, 
  129.          vs->uvnVertices[bias+2], vs->uvnVertices[bias+1]);
  130.    MAT3cross_product (babyobj->normal, vec1, vec2);
  131.    babyobj->normal[3] = 1.0;
  132.  
  133.    babyobj->traversal_index = vs->curTraversalIndex++;
  134.  
  135.    AddObjectToEndOfList (vs, babyobj);
  136. }
  137.  
  138.  
  139.  
  140.  
  141. /** OBJECT_addPoly
  142. The received xform is a composite of:
  143.    the composite modeling transform, and
  144.    the view orientation transform.
  145. Divides a polygon into its component faces. 
  146. **/
  147.  
  148. void OBJECT__addPoly 
  149.    (view_spec *vs, POLYHEDRON *poly, matrix xform, attribute_group *attrs)
  150. {
  151.    obj *babyobj;
  152.    MAT3hvec xformed_normal;
  153.    register i;
  154.    int bias;
  155.  
  156.    bias = vs->vertexCount;
  157.  
  158.    AddVertices (vs, xform, poly->vertex_list, poly->vertex_count);
  159.  
  160.    /* For each face, create a new object. */
  161.    for (i=0; i<poly->facet_count; i++) {
  162.       register j;
  163.  
  164.       babyobj = (obj*)
  165.      FALLOCalloc (vs->objectChunk, sizeof(obj), FALLOC__DONT_ZERO);
  166.       babyobj->type = objFace;
  167.       babyobj->attributes = *attrs;
  168.       babyobj->data.face.numPoints = poly->facet_list[i].vertex_count;
  169.       babyobj->data.face.points = 
  170.      (vertex_index*)
  171.         FALLOCalloc (vs->objectChunk,
  172.              sizeof(vertex_index) * babyobj->data.face.numPoints,
  173.              FALLOC__DONT_ZERO);
  174.       /* Compute normal */
  175.       MAT3mult_vec (babyobj->normal, poly->facet_list[i].normal, 
  176.             currentNormalMCtoUVNxform);
  177.  
  178.       for (j=0; j < babyobj->data.face.numPoints; j++)
  179.      babyobj->data.face.points[j] =
  180.         poly->facet_list[i].vertex_indices[j] + bias;
  181.  
  182.       babyobj->traversal_index = vs->curTraversalIndex;
  183.  
  184.       AddObjectToEndOfList (vs, babyobj);
  185.    }
  186.  
  187.    vs->curTraversalIndex++;
  188. }
  189.  
  190.  
  191.  
  192. /** OBJECT process
  193. Called after the objects have been collected.
  194. The coordinates are still in uvn.
  195. This function launches the processes of culling, clipping, etc.
  196. Which processes are actually done depends upon the current
  197.    rendering mode.
  198.  
  199. This will NEVER be called when rendermode is WIREFRAME_RAW (fastest).
  200. **/
  201. void OBJECT__process (view_spec *vs)
  202. {
  203. /*   printf ("On entry: objects are:\n");
  204.    print_objects_in_view (vs); */
  205.  
  206.    switch (currentRendermode) {
  207.     case WIREFRAME:
  208.       SPH__map_to_canon (vs);
  209.       SPH__cull (vs);
  210.       SPH__clip (vs);
  211.       SPH__generate_pdc_vertices (vs);
  212.       break;
  213.     case FLAT:
  214.     case LIT_FLAT:
  215.       SPH__map_to_canon (vs);
  216.       SPH__cull (vs);
  217.       SPH__clip (vs);
  218.       SPH__calc_intensity (vs);
  219.       SPH__zsort (vs);
  220.       SPH__generate_pdc_vertices (vs);
  221.       break;
  222.    }
  223.    
  224. /*   printf ("On exit: objects are:\n");
  225.    print_objects_in_view (vs); */
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232. /** OBJECT draw All
  233. Called after clipping, culling, sorting etc has all been done on an
  234.    entire set of objects posted to a single view.
  235. The objects by now are all in integer pdc coords!
  236. */
  237.  
  238. void OBJECT__drawAll (view_spec *vs)
  239. {
  240.    register obj *curobj;
  241.    register int v;
  242.    vertex_index *viptr;
  243.    facet *facetptr;
  244.    int shade_number, face_color;
  245.  
  246.  
  247.    for (curobj=vs->objects; curobj; curobj = curobj->next) {
  248.       switch (curobj->type) {
  249.  
  250.        case objFace:
  251.      /* CONVERT ALL VERTICES INTO SRGP VERTEX FORMAT. */
  252.      for (v = 0; v < curobj->data.face.numPoints; v++)
  253.         srgp_pdc_points[v] = vs->pdcVertices[curobj->data.face.points[v]];
  254.  
  255.      /* IF NOT WIREFRAME: */
  256.      if (currentRendermode > WIREFRAME) {
  257.         face_color = curobj->attributes.interior_color;
  258.         if (currentRendermode == LIT_FLAT) {
  259.            if IS_A_FLEXICOLORINDEX(face_color) {
  260.              shade_number = 
  261.             fabs(curobj->intensity)*NUM_OF_SHADES_PER_FLEXICOLOR;
  262.              if (shade_number == NUM_OF_SHADES_PER_FLEXICOLOR)
  263.             shade_number--;
  264.              SRGP_setColor ( 
  265.            BASE_OF_SHADE_LUT_ENTRIES
  266.              + ((face_color-2)*NUM_OF_SHADES_PER_FLEXICOLOR)
  267.             + shade_number);
  268.            SRGP_setFillStyle (SOLID);
  269.         }
  270.         else {
  271.            SRGP_setColor (face_color);
  272.            SRGP_setBackgroundColor (1);
  273.            shade_number = fabs(curobj->intensity)*64;
  274.            if (shade_number == 64)  shade_number--;
  275.            SRGP_setFillStyle (BITMAP_PATTERN_OPAQUE);
  276.            SRGP_setFillBitmapPattern (40 + shade_number);
  277.          }
  278.         }
  279.         /* Draw the interior. */
  280.         SRGP_fillPolygon (curobj->data.face.numPoints, srgp_pdc_points);
  281.      }
  282.  
  283.      /* Draw the edge. */
  284.      if (curobj->attributes.edge_flag == EDGE_VISIBLE) {
  285.         SRGP_setColor (curobj->attributes.edge_color);
  286.         SRGP_setLineWidth (curobj->attributes.edge_width);
  287.         SRGP_polygon (curobj->data.face.numPoints, srgp_pdc_points);
  288.      }
  289.  
  290.      break;
  291.       }
  292.    }
  293. }
  294.  
  295.